package com.edu.neusoft.furniture_sale.controller;

import com.edu.neusoft.furniture_sale.common.ResponseResult;
import com.edu.neusoft.furniture_sale.entity.User;
import com.edu.neusoft.furniture_sale.entity.Useraddresses;
import com.edu.neusoft.furniture_sale.service.UseraddressesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户收货地址信息表(Useraddresses)表控制层
 *
 * @author makejava
 * @since 2024-12-09 20:07:06
 */
@RestController
@Api(tags = "用户收货信息")
@RequestMapping("/api/useraddresses")
public class UseraddressesController {

    /**
     * 服务对象
     */
    @Resource
    private UseraddressesService useraddressesService;

    /**
     * 分页查询所有地址
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("分页查询所有地址")
    @GetMapping("/page")
    public ResponseResult getByPage(@RequestParam int pageNum, @RequestParam int pageSize, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        Long id = user.getId();
        return ResponseResult.okResult(useraddressesService.findByPage(pageNum, pageSize, id));
    }

    /**
     * 新增地址
     * 通过mybatis-plus底层提供的方法实现
     * @param useraddresses 实体对象
     * @return 新增结果
     */
    @ApiOperation("新增地址")
    @PostMapping
    public ResponseResult insert(@RequestBody Useraddresses useraddresses) {
        return ResponseResult.okResult(this.useraddressesService.save(useraddresses));
    }

    /**
     * 修改地址
     *
     * @param useraddresses 实体对象
     * @return 修改结果
     */
    @ApiOperation("修改地址")
    @PutMapping
    public ResponseResult update(@RequestBody Useraddresses useraddresses) {
        return ResponseResult.okResult(this.useraddressesService.updateById(useraddresses));
    }

    /**
     * 删除地址
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @ApiOperation("删除地址")
    @DeleteMapping
    public ResponseResult delete(@RequestParam("idList") List<Long> idList) {
        return ResponseResult.okResult(this.useraddressesService.removeByIds(idList));
    }

}

